package com.sidneysimmons.financially.managers;

import java.util.ArrayList;

import android.content.Context;
import android.widget.Toast;

import com.sidneysimmons.financially.Account;
import com.sidneysimmons.financially.R;
import com.sidneysimmons.financially.adapters.DatabaseAdapter;
import com.sidneysimmons.financially.exceptions.DuplicateAccountException;

public class AccountManager {
	
	private Context context;
	
	/**
	 * Utility class for all helper methods having to do with categories.
	 */
	public AccountManager(Context context) {
		this.context = context;
	}
	
	/**
	 * Returns ArrayList<Account> of all accounts.
	 */
	public ArrayList<Account> getAccounts() {
		// Get the accounts from the device database
		DatabaseAdapter database = new DatabaseAdapter(context);
		ArrayList<Account> accounts = database.getAccounts();
		
		return accounts;
	}
	
	/**
	 * Add this Account to the device's database.  Assumes the new account 
	 * has already successfully passed validation.<br><br>
	 * 
	 * If a DuplicateAccountException is thrown, shows Toast error and returns false.
	 * 
	 * @param account - the Account to add
	 * @return true if added successfully, false otherwise
	 */
	public boolean addNewAccount(Account account) {
		DatabaseAdapter database = new DatabaseAdapter(context);
		boolean successful = false;
		
		try {
			successful = database.addAccount(account);
		} catch (DuplicateAccountException e) {
			// An Account with the same name already exists in the database
			String errorText = context.getResources().getString(R.string.exception_duplicate_account);
			Toast.makeText(context, errorText, Toast.LENGTH_LONG).show();
			return false;
		}
		
		return successful;
	}
	
	/**
	 * Edit the Account in the device's database.  This assumes that the updated account has 
	 * passed front end validation.  Returns true if successful, false otherwise.
	 * 
	 * @param originalAccount
	 * @param updatedAccount
	 */
	public boolean editAccount(Account originalAccount, Account updatedAccount) {
		DatabaseAdapter database = new DatabaseAdapter(context);
		boolean successful = false;
		
		try {
			successful = database.updateAccount(originalAccount, updatedAccount);
		} catch (DuplicateAccountException e) {
			// An Account with the same name already exists in the database
			String errorText = context.getResources().getString(R.string.exception_duplicate_account);
			Toast.makeText(context, errorText, Toast.LENGTH_LONG).show();
		}
		
		return successful;
	}
	
	/**
	 * Delete an account from the device's database.
	 * 
	 * @param account
	 */
	public boolean deleteAccount(Account account) {
		DatabaseAdapter database = new DatabaseAdapter(context);
		boolean successful = false;
		
		successful = database.deleteAccount(account);
		
		if (!successful) {
			String errorText = context.getResources().getString(R.string.edit_account_dialog_text_delete_error);
			Toast.makeText(context, errorText, Toast.LENGTH_LONG).show();
		}
		
		return successful;
	}
}